<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入理解 Netty 的 ChannelHandler 与 ChannelPipeline</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            position: relative;
            overflow: hidden;
        }
        
        .hero-gradient::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23ffffff' fill-opacity='0.05'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-block::before {
            content: 'Java';
            position: absolute;
            top: 10px;
            right: 15px;
            font-size: 12px;
            color: #666;
            text-transform: uppercase;
            letter-spacing: 1px;
        }
        
        .section-title {
            position: relative;
            display: inline-block;
        }
        
        .section-title::after {
            content: '';
            position: absolute;
            bottom: -8px;
            left: 0;
            width: 60px;
            height: 4px;
            background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .feature-icon {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        
        .mermaid {
            background: white;
            padding: 2rem;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }
        
        pre code {
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            line-height: 1.6;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 1.5rem;
            border-radius: 12px;
            margin: 2rem 0;
        }
        
        .concept-card {
            background: white;
            border-radius: 16px;
            padding: 2rem;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
            transition: all 0.3s ease;
            border: 2px solid transparent;
        }
        
        .concept-card:hover {
            border-color: #667eea;
            transform: translateY(-5px);
            box-shadow: 0 15px 40px rgba(102, 126, 234, 0.15);
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-6 relative">
        <div class="max-w-6xl mx-auto text-center relative z-10">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                深入理解 Netty 的<br>
                <span class="text-yellow-300">ChannelHandler</span> 与 <span class="text-yellow-300">ChannelPipeline</span>
            </h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                探索 Netty 核心组件的设计理念，掌握高性能网络编程的精髓
            </p>
            <div class="mt-10 flex justify-center space-x-8">
                <div class="text-center">
                    <i class="fas fa-network-wired text-4xl mb-2"></i>
                    <p class="text-sm">管道模型</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-exchange-alt text-4xl mb-2"></i>
                    <p class="text-sm">事件处理</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-layer-group text-4xl mb-2"></i>
                    <p class="text-sm">链式架构</p>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-6 py-12">
        
        <!-- Introduction -->
        <div class="content-card rounded-2xl p-8 mb-12">
            <p class="text-lg leading-relaxed text-gray-700 indent-8">
                在 Netty 中，<code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">ChannelHandler</code> 和 <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">ChannelPipeline</code> 是实现数据处理和事件处理的核心组件，它们通过管道模型组织和处理不同的 I/O 事件。Netty 通过 <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">ChannelPipeline</code> 来管理多个 <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">ChannelHandler</code>，并按顺序将网络事件传递给这些处理器进行处理。这种设计方式非常灵活和高效，尤其适用于复杂的网络应用场景。
            </p>
        </div>

        <!-- Section 1: Overview -->
        <div class="mb-16">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-8">
                <i class="fas fa-cube feature-icon mr-3"></i>1. ChannelHandler 和 ChannelPipeline 概述
            </h2>
            
            <div class="grid md:grid-cols-2 gap-6">
                <div class="concept-card">
                    <h3 class="text-xl font-bold text-gray-800 mb-3 flex items-center">
                        <i class="fas fa-cog text-purple-600 mr-3"></i>ChannelHandler
                    </h3>
                    <p class="text-gray-600 leading-relaxed">
                        它是处理 I/O 事件的单个组件，Netty 提供了多种实现类型，用于不同的任务，如编解码、业务处理、异常处理等。
                    </p>
                </div>
                
                <div class="concept-card">
                    <h3 class="text-xl font-bold text-gray-800 mb-3 flex items-center">
                        <i class="fas fa-stream text-purple-600 mr-3"></i>ChannelPipeline
                    </h3>
                    <p class="text-gray-600 leading-relaxed">
                        它是 <code class="bg-gray-100 text-gray-700 px-2 py-1 rounded text-sm">ChannelHandler</code> 的容器和管理器，处理从 <code class="bg-gray-100 text-gray-700 px-2 py-1 rounded text-sm">Channel</code> 收到的所有事件。
                    </p>
                </div>
            </div>
        </div>

        <!-- Section 2: Pipeline Structure -->
        <div class="mb-16">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-8">
                <i class="fas fa-sitemap feature-icon mr-3"></i>2. ChannelPipeline 的结构
            </h2>
            
            <div class="content-card rounded-2xl p-8 mb-8">
                <p class="text-lg leading-relaxed text-gray-700 indent-8 mb-6">
                    <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">ChannelPipeline</code> 是一个链式的数据结构，它持有一组 <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">ChannelHandler</code>，并按顺序调用每个 <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">ChannelHandler</code> 处理事件。处理过程分为两个阶段：<strong class="text-purple-700">入站事件</strong>和<strong class="text-purple-700">出站事件</strong>。
                </p>
                
                <div class="code-block p-6 overflow-x-auto">
                    <pre><code class="text-sm">public class DefaultChannelPipeline extends AbstractChannelPipeline {
    private final ChannelHandlerContext head;
    private final ChannelHandlerContext tail;

    public DefaultChannelPipeline(Channel channel) {
        this.head = new DefaultChannelHandlerContext(this, null, "HEAD", true);
        this.tail = new DefaultChannelHandlerContext(this, null, "TAIL", false);
        this.head.next = this.tail;
        this.tail.prev = this.head;
    }

    @Override
    public ChannelPipeline addLast(ChannelHandler... handlers) {
        for (ChannelHandler handler : handlers) {
            addLast0(handler);
        }
        return this;
    }

    private void addLast0(ChannelHandler handler) {
        final DefaultChannelHandlerContext newCtx = new DefaultChannelHandlerContext(this, null, handler);
        tail.prev.next = newCtx;
        newCtx.prev = tail.prev;
        tail.prev = newCtx;
        newCtx.next = tail;
    }
}</code></pre>
                </div>
            </div>

            <!-- Pipeline Visualization -->
            <div class="mb-8">
                <h3 class="text-xl font-semibold text-gray-700 mb-4">Pipeline 结构可视化</h3>
                <div class="mermaid">
                    graph LR
                        A[HEAD] --> B[Handler 1]
                        B --> C[Handler 2]
                        C --> D[Handler 3]
                        D --> E[TAIL]
                        
                        style A fill:#667eea,stroke:#333,stroke-width:2px,color:#fff
                        style E fill:#764ba2,stroke:#333,stroke-width:2px,color:#fff
                        style B fill:#f093fb,stroke:#333,stroke-width:2px
                        style C fill:#f093fb,stroke:#333,stroke-width:2px
                        style D fill:#f093fb,stroke:#333,stroke-width:2px
                </div>
            </div>
        </div>

        <!-- Section 3: Handler Roles -->
        <div class="mb-16">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-8">
                <i class="fas fa-tasks feature-icon mr-3"></i>3. ChannelHandler 的角色与功能
            </h2>
            
            <div class="highlight-box">
                <h3 class="text-2xl font-bold mb-4">
                    <i class="fas fa-info-circle mr-2"></i>核心功能分类
                </h3>
                <div class="grid md:grid-cols-2 gap-4">
                    <div class="bg-white bg-opacity-20 rounded-lg p-4">
                        <h4 class="font-semibold mb-2"><i class="fas fa-download mr-2"></i>入站处理器</h4>
                        <p>用于处理从网络接收到的事件，如读取数据、解码等。</p>
                    </div>
                    <div class="bg-white bg-opacity-20 rounded-lg p-4">
                        <h4 class="font-semibold mb-2"><i class="fas fa-upload mr-2"></i>出站处理器</h4>
                        <p>用于处理向网络发送的事件，如编码数据、写数据等。</p>
                    </div>
                </div>
            </div>
            
            <div class="content